<script setup>
import { captchaAPI } from '@/api'
import { reactive, ref, onMounted } from 'vue'
import errorLogger from '@/utils/errorLogger'
import { useAdminStore } from '@/stores'
import { useRouter } from 'vue-router'

const router = useRouter()

const loginFormRef = ref()

// 验证规则
const loginRules = reactive({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
})

const loginForm = reactive({
  username: 'akbar',
  password: '123456',
  captcha: '',
})

// 获取验证码
const svgCaptcha = ref('')
const captchaId = ref('')
const getCaptcha = async () => {
  try {
    const result = await captchaAPI()
    svgCaptcha.value = result.data.captcha
    captchaId.value = result.data.id
  } catch (error) {
    errorLogger(error)
  }
}
// 首次加载验证码
onMounted(() => {
  getCaptcha()
})

// 登录
const adminStore = useAdminStore()
const handleSubmit = async (formEl) => {
  if (!formEl) return

  try {
    // validate() 方法返回一个 Promise
    await formEl.validate()

    const payload = {
      ...loginForm,
      captchaId: captchaId.value,
    }
    await adminStore.adminLogin(payload)
    router.push({ name: 'Dashboard' })
  } catch (error) {
    errorLogger(error)
    getCaptcha()
  }
}
</script>

<template>
  <div class="login-container">
    <div class="box">
      <!-- 左侧内容 -->
      <div class="left-section">
        <div class="image-box">
          <img src="@/assets/images/login.png" />
        </div>
        <div class="title">
          <h1>博客管理系统</h1>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-section">
        <h2 class="login-title">登录</h2>
        <el-form
          size="large"
          label-position="top"
          :model="loginForm"
          ref="loginFormRef"
          :rules="loginRules">
          <el-form-item label="用户名" prop="username">
            <el-input
              type="text"
              placeholder="admin or test"
              v-model="loginForm.username" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              placeholder="123456"
              show-password
              v-model="loginForm.password" />
          </el-form-item>
          <el-form-item label="验证码" prop="captcha">
            <div class="captcha-group" style="display: flex">
              <el-input type="text" v-model="loginForm.captcha" />
              <div
                class="captcha-display"
                @click="getCaptcha"
                v-html="svgCaptcha"></div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-checkbox label="记住我" />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login-btn"
              @click.prevent="handleSubmit(loginFormRef)"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  background-color: $color-bg-dark;
  @include flex-center;

  .box {
    width: 100%;
    max-width: 1200px;
    @include flex-center;

    .left-section {
      flex: 1;
      padding: 40px;
      display: flex;
      flex-direction: column;
      align-items: center;

      .image-box {
        width: 100%;
        max-width: 500px;
        margin-bottom: 40px;

        img {
          width: 100%;
          height: auto;
        }
      }

      .title {
        h1 {
          font-size: 32px;
          text-align: center;
          color: $color-white;
        }
      }
    }

    .login-section {
      background: $color-white;
      border-radius: 8px;
      padding: 40px;
      margin: 0 20px;
      width: 100%;
      max-width: 400px;

      .login-title {
        text-align: center;
        font-size: 24px;
        margin-bottom: 30px;
        color: $color-text-dark;
      }

      .el-form-item {
        margin-bottom: 18px;

        &:last-child {
          margin-bottom: 0;
        }

        .captcha-group {
          width: 100%;
          display: flex;
          align-items: center;
          gap: 10px;

          .captcha-display {
            width: 150px;
            height: 40px;
            border: 1px solid $color-border;
            border-radius: 4px;
            background-color: #cc9966;
            @include flex-center;
            cursor: pointer;
            user-select: none;
            overflow: hidden;
          }
        }

        .login-btn {
          width: 100%;
        }
      }
    }

    /* 媒体查询 - 小屏幕 */
    @media screen and (max-width: 768px) {
      & {
        .left-section {
          display: none;
        }

        .login-section {
          margin: 0 12px;
          padding: 30px 20px;
        }
      }
    }
  }
}
</style>
